/*
组织选择
*/
import React, { Suspense } from "react";
import Input from "../../framework/common/components/Input"; //单行文本
import TextArea from "../../framework/common/components/TextArea"; //多行文本
import InputNumber from "../../framework/common/components/InputNumber"; //数值
// import DatePicker from "../../framework/common/components/DatePicker"; //日期
import Select from "../../framework/common/components/Select"; //下拉选择
import Radio from "../../framework/common/components/Radio"; //单选框
import Checkbox from "../../framework/common/components/Checkbox"; //复选框
import HyperLink from "../../framework/common/components/HyperLink/HyperLink"; //超链接
import DataConnect from "../../framework/common/components/DataConnect"; //数据互联
import FileUpload from "../../framework/common/components/FileUpload"; //文件
import Calculation from "../../framework/common/components/Calculation"; //计算
import Person from "../../framework/common/components/Person"; //人员选择
import Org from "../../framework/common/components/Org"; //组织选择
import SelectCity from "../../framework/common/components/SelectCity"; //选择城市

// import MarkDown from "../../framework/common/components/MarkDown"; //mark_down
import OpinionBackFillArea from "../../framework/common/components/OpinionBackFillArea"; //回填域
import HtmlText from "../../framework/common/components/HtmlText"; //html类型
import Tip from "../../framework/common/components/Tip1"; //说明文字
import { InjectProvider, useInject } from "../../framework/common/context/inject-context";
import FieldErrorBoundary from "./FieldErrorBoundary";
import { useForm } from "../../framework/common/context/metadata-context";

const MarkDown = React.lazy(() => import("../../framework/common/components/MarkDown"));
const DatePicker = React.lazy(() => import("../../framework/common/components/DatePicker"));

/*

cell tplData.form.form_meta_data（表单设计器） 和  tplData.form.data_define（业务对象定义） 合并后的字段属性信息
isSubTableField true：子表字段 null：主表字段
codePath 拼接的对应value信息，路径为 tplData.form.form_data.children[子表code][行索引]  || tplData.form.form_data.main.[主表code]
groupCode 字段所在分组code（子表才有）
rowIndex 字段所在的行索引（子表才有）
*/
export default function ({ isSummary = false, cell, isSubTableField, codePath, groupCode, rowIndex }) {
  const injectInfo = useInject();
  const form = useForm().root;
  //这里根据字段类型类型渲染不同的组件
  let fieldType = cell.type; //字段类型
  //是否设置了默认必填项
  let isDefaultRequired = cell.isRequired === true || cell.is_required === true;
  let is_editable = cell.is_editable;
  if (isSubTableField && injectInfo?.group) {
    //子表
    //子表的is_required被放到了group.columns里面, fixme: 后面重构
    let column = injectInfo.group.columns?.find((c) => c.code === cell.code);

    isDefaultRequired = cell.isRequiredRows[rowIndex] === true || column.is_required === true;
    is_editable = column.is_editable;
  }

  if (isSummary) {
    is_editable = false;
  }
  //只读模式
  if (form.isDisplay || is_editable === false) {
    isDefaultRequired = false;
  }

  // console.log(cell.code, cell);

  /*
    字段值对应code所在数据里面的路径
    主表在 tplData.form.form_data.main
    子表在 tplData.form.form_data.children
  */
  // let codePath = "";
  let optionPath; //备选项所在路径
  let rangePath = ""; //超链接配置的固定文本数据
  let sourcePath = ""; //超链接配置的是关键字数据

  let fieldComponent = null;

  // if (isSubTableField) {
  //   //子表字段
  //   codePath = fieldCodePath; //主表字段值的key所在路径
  // } else {
  //   //主表字段
  //   codePath = "form.form_data.main." + cell.code; //主表字段值的key所在路径
  // }
  switch (
    fieldType //根据类型返回字段组件
  ) {
    case "single_text": //单行文本
      fieldComponent = <Input cell={cell} field={codePath} rowIndex={rowIndex}></Input>;
      break;
    case "multiline_text": //多行文本
      fieldComponent = <TextArea cell={cell} field={codePath} rowIndex={rowIndex}></TextArea>;
      break;
    case "number": //数值
      fieldComponent = <InputNumber cell={cell} field={codePath} rowIndex={rowIndex}></InputNumber>;
      break;
    case "date": //日期
      fieldComponent = (
        <Suspense fallback={null}>
          <DatePicker field={codePath} cell={cell} rowIndex={rowIndex}></DatePicker>
        </Suspense>
      );
      break;
    case "dropdown": //下拉选择
      //在下拉选择组件内部，会根据这两个参数进行判断选择
      if (isSubTableField) {
        //子表字段
        rangePath = "form.data_range.children." + groupCode + "." + cell.code; //固定文本每行都是一样的，不需要用rowIndex去查找每行
        sourcePath = "form.form_data.fieldSources.children." + groupCode + "[" + rowIndex + "]." + cell.code; //关键字每行都不一样，需要用rowIndex找对应行的数据
      } else {
        //主表字段
        rangePath = "form.data_range.main." + cell.code;
        sourcePath = "form.form_data.fieldSources.main." + cell.code;
      }

      fieldComponent = <Select cell={cell} field={codePath} rowIndex={rowIndex} groupCode={groupCode} isSubTableField={isSubTableField} rangePath={rangePath} sourcePath={sourcePath}></Select>;
      break;
    case "radio": //单选框
      //在单选框组件内部，会根据这两个参数进行判断选择
      if (isSubTableField) {
        //子表字段
        rangePath = "form.data_range.children." + groupCode + "." + cell.code; //固定文本每行都是一样的，不需要用rowIndex去查找每行
        sourcePath = "form.form_data.fieldSources.children." + groupCode + "[" + rowIndex + "]." + cell.code; //关键字每行都不一样，需要用rowIndex找对应行的数据
      } else {
        //主表字段
        rangePath = "form.data_range.main." + cell.code;
        sourcePath = "form.form_data.fieldSources.main." + cell.code;
      }
      fieldComponent = <Radio field={codePath} cell={cell} isSubTableField={isSubTableField} groupCode={groupCode} rowIndex={rowIndex} rangePath={rangePath} sourcePath={sourcePath}></Radio>;
      break;
    case "checkbox_group": //复选框
      //在复选框组件内部，会根据这两个参数进行判断选择
      if (isSubTableField) {
        //子表字段
        rangePath = "form.data_range.children." + groupCode + "." + cell.code; //固定文本每行都是一样的，不需要用rowIndex去查找每行
        sourcePath = "form.form_data.fieldSources.children." + groupCode + "[" + rowIndex + "]." + cell.code; //关键字每行都不一样，需要用rowIndex找对应行的数据
      } else {
        //主表字段
        rangePath = "form.data_range.main." + cell.code;
        sourcePath = "form.form_data.fieldSources.main." + cell.code;
      }
      fieldComponent = (
        <Checkbox
          // displayFormatter={(value = "") => {
          //   if (typeof value === "string" || value instanceof String) {
          //     return value?.split(",");
          //   }
          //   return value;
          // }}
          // valueFormatter={(value = []) => {
          //   if (Array.isArray(value) && !Array.isArray(value[0])) {
          //     return value?.join(",");
          //   }
          //   return value;
          // }}
          groupCode={groupCode}
          field={codePath}
          cell={cell}
          isSubTableField={isSubTableField}
          rangePath={rangePath}
          sourcePath={sourcePath}
        ></Checkbox>
      );
      break;
    case "hyperlink": //超链接
      //在超链接组件内部，会根据这两个参数进行判断选择
      if (isSubTableField) {
        //子表字段
        rangePath = "form.data_range.children." + groupCode + "." + cell.code; //固定文本每行都是一样的，不需要用rowIndex去查找每行
        sourcePath = "form.form_data.fieldSources.children." + groupCode + "[" + rowIndex + "]." + cell.code; //关键字每行都不一样，需要用rowIndex找对应行的数据
      } else {
        //主表字段
        rangePath = "form.data_range.main." + cell.code;
        sourcePath = "form.form_data.fieldSources.main." + cell.code;
      }
      fieldComponent = <HyperLink cell={cell} field={codePath} isSubTableField={isSubTableField} rowIndex={rowIndex} rangePath={rangePath} sourcePath={sourcePath}></HyperLink>;
      break;
    case "calculation": //计算
      fieldComponent = <Calculation cell={cell} field={codePath} rowIndex={rowIndex}></Calculation>;
      break;
    case "select_person": //人员选择
      fieldComponent = <Person cell={cell} field={codePath} rowIndex={rowIndex}></Person>;
      break;
    case "select_org": //组织选择
      fieldComponent = <Org cell={cell} field={codePath} rowIndex={rowIndex}></Org>;
      break;
    case "data_connect": //数据互联
      fieldComponent = <DataConnect cell={cell} field={codePath} isSubTableField={isSubTableField} groupCode={groupCode} rowIndex={rowIndex}></DataConnect>;
      break;
    case "file": //文件
      fieldComponent = <FileUpload cell={cell} field={codePath} rowIndex={rowIndex}></FileUpload>;
      break;
    case "select_city": //选择城市
      fieldComponent = <SelectCity cell={cell} field={codePath} rowIndex={rowIndex}></SelectCity>;
      break;
    case "mark_down": //mark_down
      fieldComponent = (
        <Suspense fallback={null}>
          <MarkDown cell={cell} field={codePath} rowIndex={rowIndex}></MarkDown>
        </Suspense>
      );
      break;
    case "opinion_backfill_area": //回填域
      fieldComponent = <OpinionBackFillArea cell={cell} field={codePath} rowIndex={rowIndex}></OpinionBackFillArea>;
      break;
    case "html_text": //html类型
      fieldComponent = <HtmlText isSubTableField={isSubTableField} cell={cell} field={codePath} rowIndex={rowIndex}></HtmlText>;
      break;
    case "tip": //说明文字
      fieldComponent = <Tip field={codePath} cell={cell}></Tip>;
      break;
    default:
      console.warn("当前组件type未知", cell);
      fieldComponent = null;
  }
  return (
    <InjectProvider value={{ isDefaultRequired: isDefaultRequired, isEditable: is_editable, rowIndex: rowIndex }}>
      <FieldErrorBoundary>{fieldComponent}</FieldErrorBoundary>
    </InjectProvider>
  );
}
